<?php
    include "../database.php";

    $project_name = $_POST["name"];
    $project_id = $_POST["project_id"];
    $description = $_POST["description"];
    $product_owner = $_POST["leader"];
    $scrum_master = $_POST["master"];
    
    $queryTest = "SELECT * FROM project WHERE project_name = '$project_name'";
    $res = executeQuery($queryTest);
    $numRows = mysql_num_rows($res);
    $row = mysql_fetch_array($res);
    
    //Projekt spremenimo samo, če projekt s takim imenom še ne obstaja v bazi
    if($numRows==0 || ($numRows==1 && $row['project_id'] == $project_id)){
    
        $query = "UPDATE  project SET  project_name = '$project_name', description = '$description', product_owner = '$product_owner' ,scrum_master = '$scrum_master' WHERE  project_id = '$project_id'";
        $res = executeQuery($query);

        //Izbrisemo vse uporabnike povezane s projektoms
        $query = "DELETE FROM project_user WHERE project_id = '$project_id'";
        executeQuery($query);

        //Vnesemo vse uporabnike, vnesene preko checkboxa
        $query = "INSERT INTO project_user (project_id, user_id) VALUES ";
        if(isset($_POST['users']) && array_count_values($_POST['users'])>0 ){
            foreach ($_POST['users'] as $user_id){
                $query .= "('$project_id', '$user_id'), ";
            }
            $query = rtrim($query, ', ');
            executeQuery($query);
        }
        $msg = message(200);
        $data = array('msg' => "$msg", 'success' => '1');
        //header('Location: ../control_panel.php');
    }
    else{
        $msg = message(103);
        $success = 0;
        $data = array('msg' => "$msg", 'success' => '0');
    }
    echo json_encode($data);
    //echo "Projekt s tem imenom že obstaja!";
?>
